package com.zhugang.week13.tree;


import java.util.ArrayList;
import java.util.List;

/**
 * @program algorithms
 * @description: inorderTraversal
 * @author: chanzhugang
 * @create: 2022/12/13 10:49
 */
public class InorderTraversal {

    /**
     * 94 二叉树的中序遍历
     * https://leetcode.cn/problems/binary-tree-inorder-traversal/
     *
     * @param root
     * @return
     */
    List<Integer> result = new ArrayList<>();

    public List<Integer> inorderTraversal(TreeNode root) {
        inorder(root);
        return result;
    }

    private void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        result.add(root.val);
        inorder(root.right);
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}